package org.ioc.com.book_test1.Interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.ioc.com.book_test1.constant.Constants;
import org.ioc.com.book_test1.model.UserInfo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        UserInfo attribute = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
        if (attribute==null||attribute.getId()<0){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
